package com.woniuxy.micropermissionserver.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.woniuxy.microcommonserver.util.JWTUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class TokenUtil {

    public static String resolveToken() {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        String authentication = request.getHeader("authentication");
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(JWTUtil.SECURITY)).build();
        DecodedJWT decodedJWT = verifier.verify(authentication);
        return decodedJWT.getClaim("loginName").asString();
    }
}
